#include "boundedbuffer.h"

void* producer(void);
void* consumer(void);

struct BB* bb;

int
main(int argc, char *argv[])
{
	int nconsumers, nproducers;
	int buffer_size;
	int i, t;
	int* thread_consumers, * thread_producers;
	void *pg;

	if (argc <= 2) exit();
	//params: nconsumers, nproducers, buffer_size
	nconsumers = atoi(argv[1]);
	nproducers = atoi(argv[2]);
	buffer_size = atoi(argv[3]);

	bb = BB_create(buffer_size);

	thread_consumers = (int*)malloc(nconsumers * sizeof(int));
	thread_producers = (int*)malloc(nproducers * sizeof(int));

	t=0;
	for (i = 0; i<nconsumers; i++){
		pg = malloc(1024);
		thread_consumers[i] = thread_create(&consumer, pg, 1024);
	}

	for (i = 0; i<nproducers; i++){
		pg = malloc(1024);
		thread_producers[i] = thread_create(&producer, pg, 1024);
	}

	thread_exit((void *)0);
	return 0;
}

void * producer(){
	int i;
	for (i=0; i<100; i++){
		semaphore_put(bb, (void*) (i+thread_getid()*10));
		printf(1, "producer %d put %d in bb\n", thread_getid(), (i+thread_getid()*10));
	}
	thread_exit((void*) 0);
	return 0;
}

void * consumer(){
	int i;
	int product;
	for (i=0; i<100; i++){
		product = (int) semaphore_pop(bb);
		printf(1, "consumer %d got %d from bb\n", thread_getid(), product);
	}
	thread_exit((void*) 0);
	return 0;
}